package net.sk.china.city.service.impl;

import net.sk.china.city.domain.ArUserApprove;
import net.sk.china.city.domain.dto.approve.ApproveQueryDTO;
import net.sk.china.city.domain.dto.approve.VolunteerApproveDTO;
import net.sk.china.city.mapper.ArTeamMapper;
import net.sk.china.city.mapper.ArUserApproveMapper;
import net.sk.china.city.mapper.UserMapper;
import net.sk.china.city.service.IArUserApproveService;
import net.sk.china.city.utils.PlatformIdHolder;
import net.sk.china.city.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 志愿者审核Service业务层处理
 * 
 * @author yangguanjin
 * @date 2024-04-17
 */
@Service
public class ArUserApproveServiceImpl implements IArUserApproveService
{
    @Autowired
    private ArUserApproveMapper arUserApproveMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ArTeamMapper arTeamMapper;

    /**
     * 查询志愿者审核
     * 
     * @param pkId 志愿者审核主键
     * @return 志愿者审核
     */
    @Override
    public ArUserApprove selectArUserApproveByPkId(Long pkId)
    {
        return arUserApproveMapper.selectArUserApproveByPkId(pkId);
    }

    /**
     * 查询志愿者审核列表
     * 
     * @param approveQueryDTO 志愿者审核
     * @return 志愿者审核
     */
    @Override
    public List<ArUserApprove> selectArUserApproveList(ApproveQueryDTO approveQueryDTO)
    {
        String platformId = PlatformIdHolder.getPlatformId();
        approveQueryDTO.setPlatformId(platformId);
        return arUserApproveMapper.selectUserApproveTeamList(approveQueryDTO);
    }

    /**
     * 新增志愿者审核
     * 
     * @param arUserApprove 志愿者审核
     * @return 结果
     */
    @Override
    public int insertArUserApprove(ArUserApprove arUserApprove)
    {
        return arUserApproveMapper.insertArUserApprove(arUserApprove);
    }

    /**
     * 修改志愿者审核
     * 
     * @param arUserApprove 志愿者审核
     * @return 结果
     */
    @Override
    public int updateArUserApprove(ArUserApprove arUserApprove)
    {
        return arUserApproveMapper.updateArUserApprove(arUserApprove);
    }

    /**
     * 批量删除志愿者审核
     * 
     * @param pkIds 需要删除的志愿者审核主键
     * @return 结果
     */
    @Override
    public int deleteArUserApproveByPkIds(Long[] pkIds)
    {
        return arUserApproveMapper.deleteArUserApproveByPkIds(pkIds);
    }

    /**
     * 删除志愿者审核信息
     * 
     * @param pkId 志愿者审核主键
     * @return 结果
     */
    @Override
    public int deleteArUserApproveByPkId(Long pkId)
    {
        return arUserApproveMapper.deleteArUserApproveByPkId(pkId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int approveVolunteer(VolunteerApproveDTO volunteerApprove) {
        Long teamApproveId = volunteerApprove.getTeamApproveId();
        arUserApproveMapper.passTeamApprove(teamApproveId);
//        // 1. 先通过队伍审核
//        Long teamApproveId = volunteerApprove.getTeamApproveId();
//        arUserApproveMapper.passTeamApprove(teamApproveId);
//
//        // 2. 再通过志愿者审核
//        Long volunteerApproveId = volunteerApprove.getVolunteerApproveId();
//        arUserApproveMapper.passVolunteerApprove(volunteerApproveId);
//
//        // 3. 获取审核信息
//        ArUserApprove userApprove = arUserApproveMapper.selectArUserApproveByPkId(teamApproveId);
//
//        // 4. 写回审核信息
//        // 4.1 写回表单信息到用户表（user）
//        Long userId = volunteerApprove.getUserId();
//        User user = userMapper.selectUserByPkId(userId);
//        if (user == null) {
//            throw new ServiceException("操作不当导致功能异常：approve关联的user不存在");
//        }
//        // 若有任一队长同意其请求，就已经是志愿者
//        if (user.getRole() == 0) {
//            user = userApprove.toUser();
//            userMapper.updateUserToVolunteer(user);
//        }
//        // 4.2 用户加入到队伍中
//        Long teamId = userApprove.getTeamId();
//        String platformId = userApprove.getPlatformId();
//        arTeamMapper.addUserToTeam(teamId, userId, platformId);
//
//        // 4.3 清除redis缓存，让用户端重载数据到缓存
//        Long userPkId = user.getPkId();
//        redisCache.deleteObject("login:info:" + userPkId);
        return 1;
    }

    public int denyVolunteer(VolunteerApproveDTO volunteerApprove) {
        // 1. 将对应的队伍申请置为 2（审核未通过）
        Long teamApproveId = volunteerApprove.getTeamApproveId();
        arUserApproveMapper.denyTeamApprove(teamApproveId);
        // 2. 检查是否全部队伍申请被置为 2（审核未通过），如果是，把志愿者申请置为 2（审核未通过）
//        Long volunteerApproveId = volunteerApprove.getVolunteerApproveId();
//        int count = arUserApproveMapper.countPassTeamApproveOrProcessed(volunteerApproveId);
//        if (count <= 0) {
//            arUserApproveMapper.denyVolunteerApprove(volunteerApproveId);
//        }
        return 1;
    }
}
